package team.seekers.hula.common.utils;

import org.apache.commons.lang3.StringUtils;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;

import java.text.MessageFormat;

public class AssertUtils {

    public static ApiException newException(ApiExceptionEnum exceptionEnum, String message, Object... args) {
        if (StringUtils.isNotBlank(message)) {
            return new ApiException(exceptionEnum, MessageFormat.format(message, args));
        }
        return new ApiException(exceptionEnum);
    }

    public static void isTrue(boolean expression, ApiExceptionEnum exceptionEnum) {
        isTrue(expression, exceptionEnum, null);
    }

    public static void isTrue(boolean expression, ApiExceptionEnum exceptionEnum, String message, Object... args) {
        if (!expression) {
            throw newException(exceptionEnum, message, args);
        }
    }

    public static void notNull(Object obj, ApiExceptionEnum exceptionEnum) {
        notNull(obj, exceptionEnum, null);
    }

    public static void notNull(Object obj, ApiExceptionEnum exceptionEnum, String message, Object... args) {
        if (obj == null) {
            throw newException(exceptionEnum, message, args);
        }
    }

}
